package com.sanyou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sanyou.mapper.EquipmentMapper;
import com.sanyou.mapper.EquipmentRecordsMapper;
import com.sanyou.mapper.FactoryMapper;
import com.sanyou.mapper.UserEquipmentMapper;
import com.sanyou.pojo.Equipment;
import com.sanyou.pojo.EquipmentRecords;
import com.sanyou.pojo.Factory;
import com.sanyou.pojo.UserEquipment;
import com.sanyou.pojo.vo.EquipmentVo;
import com.sanyou.service.EquipmentService;
import com.sanyou.utils.PagedResult;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * User: asus
 * Date: 2021/6/5
 * Time: 21:35
 * Version:V1.0
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private EquipmentRecordsMapper equipmentRecordsMapper;

    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private UserEquipmentMapper userEquipmentMapper;

    @Autowired
    private Sid sid;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void addEquipment(Equipment equipment) {
        String id = sid.nextShort();
        equipment.setId(id);
        equipment.setCreatetime(new Date());
        equipment.setEquipStatus((byte)1);
        equipment.setDeleteMark((byte)0);

        String subFactoryId = equipment.getSubFactoryId();
        Factory workshop = factoryMapper.selectByPrimaryKey(subFactoryId);
        equipment.setEquipHealthLimit(workshop.getEquipHealthLimit());
        equipment.setEquipSubhealthLimit(workshop.getEquipSubHealthLimit());
        equipment.setEquipCycle(workshop.getEquipCycle());

        equipmentMapper.insert(equipment);

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult query(EquipmentVo equipmentVo, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        List<EquipmentVo> list = equipmentMapper.query(equipmentVo);

        for (EquipmentVo vo : list) {
            System.out.println("返回设备："+vo.getEquipType());
            String factoryId = vo.getFactoryId();
            if(StringUtils.isNotBlank(factoryId)){
                Factory factory = factoryMapper.selectByPrimaryKey(factoryId);
                if(factory != null)
                    vo.setFactoryName(factory.getFactoryName());
            }
            String subFactoryId = vo.getSubFactoryId();
            if(StringUtils.isNotBlank(subFactoryId)){
                Factory factory = factoryMapper.selectByPrimaryKey(subFactoryId);
                if(factory != null)
                    vo.setSubFactoryName(factory.getFactoryName());
            }

            if(vo.getEquipStatus() == 1)
                vo.setEquipStatusName("未上线");
            else if(vo.getEquipStatus() == 2)
                vo.setEquipStatusName("在线");
            else if(vo.getEquipStatus() == 3)
                vo.setEquipStatusName("已离线");
            if(vo.getEquipType()==2){
                vo.setEquipTypeName("动态检测设备");
            }
            else if (vo.getEquipType()==1){
                vo.setEquipTypeName("铜离子检测设备");
            }

        }

        PageInfo<EquipmentVo> pageList = new PageInfo<>(list);

        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

   /* @Override
    public PagedResult querySearch(EquipmentVo equipmentVo, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<EquipmentVo> list = equipmentMapper.querySearch(equipmentVo);

        for (EquipmentVo vo : list) {
            String factoryId = vo.getFactoryId();
            if(StringUtils.isNotBlank(factoryId)){
                Factory factory = factoryMapper.selectByPrimaryKey(factoryId);
                if(factory != null)
                    vo.setFactoryName(factory.getFactoryName());
            }
            String subFactoryId = vo.getSubFactoryId();
            if(StringUtils.isNotBlank(subFactoryId)){
                Factory factory = factoryMapper.selectByPrimaryKey(subFactoryId);
                if(factory != null)
                    vo.setSubFactoryName(factory.getFactoryName());
            }

            if(vo.getEquipStatus() == 1)
                vo.setEquipStatusName("未上线");
            else if(vo.getEquipStatus() == 2)
                vo.setEquipStatusName("在线");
            else if(vo.getEquipStatus() == 3)
                vo.setEquipStatusName("已离线");

        }

        PageInfo<EquipmentVo> pageList = new PageInfo<>(list);

        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;

    }
*/
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void enableOrUnEnableEquip(String id, int mark) {
        Equipment equipment = new Equipment();
        equipment.setId(id);
        equipment.setEnableMark((byte)mark);
        equipment.setUpdatetime(new Date());

        Example example = new Example(Equipment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        equipmentMapper.updateByExampleSelective(equipment,example);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateEquipInfo(Equipment equipment,String userId) {

        Equipment equipment1 = equipmentMapper.selectByPrimaryKey(equipment.getId());
        if(equipment1 != null){
            String content = "";
            if(equipment.getEquipCycle().intValue() != equipment1.getEquipCycle().intValue()){
                content =  "周期由" + equipment1.getEquipCycle().intValue() + "修改为" +
                        equipment.getEquipCycle().intValue();
            }
            if(equipment.getEquipType()==0&&equipment.getEquipHealthLimit().doubleValue() != equipment1.getEquipHealthLimit().doubleValue()){
                if(content.length() > 0)
                    content += ",";
                content =  content + "健康值上限由" + equipment1.getEquipHealthLimit().doubleValue() + "修改为" +
                        equipment.getEquipHealthLimit().doubleValue();
            }
            if(equipment.getEquipType()==0&&equipment.getEquipSubhealthLimit().doubleValue() != equipment1.getEquipSubhealthLimit().doubleValue()){
                if(content.length() > 0)
                    content += ",";
                content =  content + "亚健康值由" + equipment1.getEquipSubhealthLimit().doubleValue() + "修改为" +
                        equipment.getEquipSubhealthLimit().doubleValue();
            }
            if(equipment.getEquipType()==1||equipment.getEquipCycle().intValue() != equipment1.getEquipCycle().intValue() ||
                    equipment.getEquipHealthLimit().doubleValue() != equipment1.getEquipHealthLimit().doubleValue() ||
                    equipment.getEquipSubhealthLimit().doubleValue() != equipment1.getEquipSubhealthLimit().doubleValue()){
                EquipmentRecords equipmentRecords = new EquipmentRecords();
                String id = sid.nextShort();
                equipmentRecords.setId(id);
                equipmentRecords.setEquipNo(equipment.getEquipNo());
                equipmentRecords.setUserId(userId);
                equipmentRecords.setContent(content);
                equipmentRecords.setCreatetime(new Date());
                equipmentRecordsMapper.insert(equipmentRecords);
            }

            if (!Objects.equals(equipment.getSubFactoryId(), equipment1.getSubFactoryId())) {
                Factory workshop = factoryMapper.selectByPrimaryKey(equipment.getSubFactoryId());
                equipment.setEquipHealthLimit(workshop.getEquipHealthLimit());
                equipment.setEquipSubhealthLimit(workshop.getEquipSubHealthLimit());
                equipment.setEquipCycle(workshop.getEquipCycle());
            }
        }


        equipment.setUpdatetime(new Date());

        if(equipment.getDeleteMark() != null && equipment.getDeleteMark() == 1)
            equipment.setDeletetime(new Date());

        Example example = new Example(Equipment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",equipment.getId());
        equipmentMapper.updateByExampleSelective(equipment,example);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<EquipmentVo> getEquipmentTree() {

        List<EquipmentVo> equipTree = new ArrayList<>();

        List<EquipmentVo> factoryList = equipmentMapper.getFactoryList();

        if(factoryList != null && factoryList.size() > 0){
            for (EquipmentVo factoryVo : factoryList) {

                String factoryId = factoryVo.getId();
                List<EquipmentVo> subFactoryList = equipmentMapper.getSubFactoryList(factoryId);
                if(subFactoryList != null && subFactoryList.size() > 0){
                    for (EquipmentVo subFactoryVo : subFactoryList) {
                        String subFactoryId = subFactoryVo.getId();

                        Example example = new Example(Equipment.class);
                        Example.Criteria criteria = example.createCriteria();
                        criteria.andEqualTo("factoryId",factoryId);
                        criteria.andEqualTo("subFactoryId",subFactoryId);
                        List<Equipment> equipmentList = equipmentMapper.selectByExample(example);

                        List<EquipmentVo> equipmentVos = new ArrayList<>();
                        for (Equipment equipment : equipmentList) {
                            EquipmentVo equipmentVo = new EquipmentVo();
                            BeanUtils.copyProperties(equipment,equipmentVo);
                            equipmentVo.setLabel(equipment.getEquipNo());
                            equipmentVos.add(equipmentVo);
                        }
                        //subFactoryVo.setChildren(equipmentVos);
                    }
                }
                factoryVo.setChildren(subFactoryList);
            }
            equipTree = factoryList;
        }


        return equipTree;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Equipment> getUserEquip(String userId,Byte equipType) {

        /*System.out.println("用户："+userId);
        Example example = new Example(UserEquipment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        List<UserEquipment> userEquipments = userEquipmentMapper.selectByExample(example);

        if(userEquipments.size() > 0){
            List<Equipment> equipmentList = new ArrayList<>();

            for (UserEquipment userEquipment : userEquipments) {
                String equipmentId = userEquipment.getEquipId();
                Example equipExample = new Example(Equipment.class);
                Example.Criteria equipCriteria = equipExample.createCriteria();
                equipCriteria.andEqualTo("equipType",equipType);
                equipCriteria.andEqualTo("id",equipmentId);
                Equipment equipment = equipmentMapper.selectOneByExample(equipExample);
                if ("".equals(equipment)||equipment==null){
                    System.out.println("设备已删除"+equipmentId);

                }
              else{  System.out.println("设备： "+equipment.getId());
                equipmentList.add(equipment);}

            }

            return equipmentList;
        }else{
            return new ArrayList<>();
        }*/
        return equipmentMapper.getEquipmentByUserId(userId,equipType.toString());
    }

    @Override
    public List<Equipment> listEquipmentByUserId(String userId) {
        return equipmentMapper.getEquipmentByUserId(userId,null);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteEquip(Equipment equipment) {
        equipment.setDeleteMark((byte)1);
        equipmentMapper.updateByPrimaryKeySelective(equipment);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateAddress(Equipment equipment) {
        equipmentMapper.updateByPrimaryKeySelective(equipment);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateEquipmentBySubFactory(Factory subFactory) {
        //获取车间下所属设备
        Example example = new Example(Equipment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("subFactoryId",subFactory.getId())
                .andEqualTo("deleteMark",0);
        List<Equipment> equipmentList = equipmentMapper.selectByExample(example);

        //更新设备
        for (Equipment equipment : equipmentList) {
            equipment.setEquipHealthLimit(subFactory.getEquipHealthLimit());
            equipment.setEquipSubhealthLimit(subFactory.getEquipHealthLimit());
            equipment.setEquipCycle(subFactory.getEquipCycle());
            equipmentMapper.updateByPrimaryKeySelective(equipment);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Equipment> getEquip(String factoryId) {

        Example example = new Example(Equipment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("factoryId",factoryId);
        criteria.andEqualTo("deleteMark",0);

        List<Equipment> equipmentList = equipmentMapper.selectByExample(example);

        return equipmentList;
    }

    @Override
    public List<Equipment> getEquipBySubFactoryId(String subFactoryId) {
        Example example = new Example(Equipment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("subFactoryId",subFactoryId);
        criteria.andEqualTo("deleteMark",0);

        List<Equipment> equipmentList = equipmentMapper.selectByExample(example);

        return equipmentList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Equipment getEquipById(String id) {

        Equipment equipment = equipmentMapper.selectByPrimaryKey(id);
        return equipment;
    }

    @Override
    public Equipment getEquipmentByCode(String equipNo) {
        Example example = new Example(Equipment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("equipNo",equipNo);
        criteria.andEqualTo("deleteMark",0);

        List<Equipment> equipmentList = equipmentMapper.selectByExample(example);
        if(equipmentList!=null && equipmentList.size()>0){
            return equipmentList.get(0);
        }
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<EquipmentVo> getEquipmentFactoryList() {
        return equipmentMapper.getEquipmentFactoryList();
    }

    @Override
    public List<Equipment> getEquipmentFactoryListbySubFac(String subFacId) {


        return equipmentMapper.getEquipmentFactoryListbySubFac(subFacId);
    }
}
